package com.jdyinfo.ic.controller;

import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdyinfo.ic.mysql.entity.Account;
import com.jdyinfo.ic.service.AccountService;
import com.jdyinfo.ic.util.IcUtil;

import io.swagger.annotations.Api;

@Api(value="/user",tags="dfds")
@RestController
@EnableAutoConfiguration
@RequestMapping("/")
public class AccountController {
	Logger logger = LoggerFactory.getLogger(AccountController.class);
	
	@Autowired
	ObjectMapper objectMapper;
	
	@Autowired
	AccountService accountService;
	
	@PostMapping(value = "/account", consumes = "application/json; charset=utf-8")
	int createAccount(@RequestBody Account account) {
		int tenantId = 1;
		byte status = 1;
		Timestamp nowTimestamp = IcUtil.nowTimestamp();
		String createdBy = "-1";
		String lastUpdatedBy = "-1";
		
		account.setTenantId(tenantId);
		account.setStatus(status);
		account.setCreatedBy(createdBy);
		account.setCreatedDate(nowTimestamp);
		account.setLastUpdatedBy(lastUpdatedBy);
		account.setLastUpdatedDate(nowTimestamp);
		
		return accountService.createAccount(account);
	}
	
	@GetMapping(value = "/account/{name}")
	Account getAccount(@PathVariable("name") String name) {
		return accountService.findAccount(name);
	}
	
	@PostMapping("/account/{name}")
	int changeAccountPassword(@PathVariable("name") String name, @RequestBody Account account) {
		String lastUpdatedBy = "-1";
		Timestamp lastUpdatedDate = IcUtil.nowTimestamp();
		account.setName(name);
		account.setLastUpdatedBy(lastUpdatedBy);
		account.setLastUpdatedDate(lastUpdatedDate);
		
		return accountService.changeAccountPassword(account);
	}
	
	@GetMapping("/account/{name}/status/{status}")
	int changeAccountStatus(@PathVariable("name") String name, @PathVariable("status") byte status) {
		String lastUpdatedBy = "-1";
		Timestamp lastUpdatedDate = IcUtil.nowTimestamp();
		
		Account account = new Account();
		account.setName(name);
		account.setStatus(status);
		account.setLastUpdatedBy(lastUpdatedBy);
		account.setLastUpdatedDate(lastUpdatedDate);
		
		return accountService.changeAccountStatus(account);
	}
	
	/*@ApiOperation(value="",notes="")
	@PostMapping("/user/{id}")
	User updateUser(@PathVariable String id, @RequestBody User user) {
		return userService.findUserByXXX(xxx).get(0);
	}*/
}
